<template>
  <transition mode="out-in">
    <div class="login" v-if="operate === 'login'">login</div>
    <div class="register" v-else="operate === 'register'">register</div>
  </transition>

  <button @click="operate = (operate === 'login' ? 'register' : 'login')">登录/注册</button>

  <hr>

  <div>
    <input type="text" v-model="name">
    <button @click="addUser">添加用户</button>
  </div>
  <ul>
    <TransitionGroup enter-active-class="animate__animated animate__bounceIn"
      leave-active-class="animate__animated animate__bounceOut">
      <li v-for="(item, index) in users" :key="item.id">
        <span>{{ item.name }}</span>
        <span style="margin:10px" @click="delUser(index)">x</span>
      </li>
    </TransitionGroup>
  </ul>



  <hr>


  <Transition enter-active-class="animate__animated animate__lightSpeedInRight"
    leave-active-class="animate__animated animate__lightSpeedOutLeft">
    <div class="box4" v-show="isBool4"></div>
  </Transition>
  <button @click="isBool4 = !isBool4">切换动画</button>

  <Transition name="topSlide">
    <div class="box3" v-show="isBool3"></div>
  </Transition>
  <button @click="isBool3 = !isBool3">切换动画</button>

  <Transition name="slide">
    <div class="box2" v-show="isBool2"></div>
  </Transition>
  <button @click="isBool2 = !isBool2">切换动画</button>


  <Transition>
    <div class="box1" v-show="isBool1"></div>
  </Transition>
  <button @click="isBool1 = !isBool1">切换动画</button>



  <div>
    <div class="tabs">
      <span @click="flag = 'video'" :class="{ active: flag === 'video' ? true : false }">视频</span>
      <span @click="flag = 'car'" :class="{ active: flag === 'car' ? true : false }">汽车</span>
      <!-- <span @click="flag = 'game'" :class="{ active: flag === 'game' ? true : false }">游戏</span> -->
      <span @click="flag = 'gupiao'" :class="{ active: flag === 'gupiao' ? true : false }">股票</span>
    </div>
    <div class="content">
      <!-- 
        动态组价（可以在多个组件间来回切换）
        切换(在挂载和卸载间切换)
        :exclude="['GuPiao']"// 排除GuPiao不被缓存
        :max="2" 最多缓存2个组件实例。依赖LRU缓存机制
      -->
      <KeepAlive :max="2">
        <Transition>
          <component :is="comInstance" />
        </Transition>
      </KeepAlive>

    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import CarCom from './components/CarCom.vue'
import GameCom from './components/GameCom.vue'
import VideoCom from './components/VideoCom.vue'
import GuPiaoCom from './components/GuPiaoCom.vue'

const flag = ref('video')
const isBool1 = ref(true)
const isBool2 = ref(true)
const isBool3 = ref(true)
const isBool4 = ref(true)
const name = ref('')
const operate = ref('login')


const users = ref([
  { id: 1, name: '谢起珍' },
  { id: 2, name: '程泉峰' },
  { id: 3, name: '林加福' },
])

const delUser = (index) => {
  users.value.splice(index, 1)
}

const addUser = () => {
  users.value.push({ id: Date.now(), name: name.value })
  name.value = ''
}

const comInstance = computed(() => {
  // 项目有大量的if else分支语句，可以通过k:v键值对形式去优化
  // if (flag.value === 'video') {
  //   return VideoCom
  // }
  // if (flag.value === 'game') {
  //   return GameCom
  // }
  // if (flag.value === 'gupiao') {
  //   return GuPiaoCom
  // }
  // if (flag.value === 'car') {
  //   return CarCom
  // }

  const obj = {
    video: VideoCom,
    game: GameCom,
    gupiao: GuPiaoCom,
    car: CarCom,
  }
  return obj[flag.value]
})


// 是一个组件实例
// console.log(VideoCom)

</script>

<style lang="scss" scoped>
.login {
  width: 80px;
  height: 80px;
  border: 2px solid #48695f;
}


.register {
  width: 80px;
  height: 80px;
  border: 2px solid #484a69;
}




// 从上边进来，左边出去
.topSlide-enter-from {
  transform: translateY(-100%);
}

.topSlide-enter-to {
  transform: translateY(0);
}

.topSlide-leave-from {
  transform: translateX(0);
}

.topSlide-leave-to {
  transform: translateX(-100%);
}

.topSlide-enter-active,
.topSlide-leave-active {
  transition: 0.5s all;
}








// 从左进来（enter即显示），从右出去(leave即隐藏)
// 刚进来
.slide-enter-from {
  transform: scale(0) translateX(-999%);
}

// 已经进来
.slide-enter-to {
  transform: scale(1) translateX(0);
}


// 刚出场
.slide-leave-from {
  transform: scale(1) translateX(0);
}

// 已经出场
.slide-leave-to {
  transform: scale(0) translateX(999%);
}


.slide-enter-active,
.slide-leave-active {
  transition: 0.5s all ease;
}



.v-enter-from,
.v-leave-to {
  opacity: 0;
  transform: scale(0.5);
}

.v-enter-to,
.v-leave-from {
  opacity: 1;
  transform: scale(1);
}

.v-enter-active,
.v-leave-active {
  transition: 0.5s all ease;
}


.box1 {
  width: 200px;
  height: 200px;
  background-color: pink;
}

.box2 {
  width: 200px;
  height: 200px;
  background-color: skyblue;
}

.box3 {
  width: 200px;
  height: 200px;
  background-color: purple;
}

.box4 {
  width: 200px;
  height: 200px;
  background-color: orange;
}

.tabs {
  padding: 10px;

  span {
    padding: 0 20px;
  }

  .active {
    border-bottom: 2px solid orange;
  }

}
</style>